window.onload = function(){

	function setClick(obj){

		var Tobj = getPhone();
		var Inputval;
		if(obj.attr("id") == "Sphone"){
			//设置input的值
			Inputval = stringChangArr(Tobj.startp,Tobj.numdiff);
		}else{
			Inputval = stringChangArr(Tobj.endp,Tobj.numdiff);
		}
		
		var alen = Tobj.startp.length;
		var len1 = (Tobj.numdiff)*10;
		var len2 = (Tobj.startp.length-Tobj.numdiff)*10;
		obj.find("input").eq(0).css("width",len1+"px");

		obj.find("input").eq(1).attr("maxlength",Tobj.startp.length-Tobj.numdiff)
		obj.find("input").eq(1).css("width",len2+"px");
		
		for(var i=0; i<Inputval.length;i++){
			obj.find("input").eq(i).val(Inputval[i]);
		}
		
		obj.removeClass("showS").addClass("hideS");
		obj.find("input").addClass("showI").removeClass("hideI");
		
		obj.find("input").eq(0).animate({
			left: '5px'
		});
		obj.find("input").eq(1).animate({
			left: (len1+20)+'px'
		});

        obj.find("input").eq(1).focus();
	}
	
	//获取号段，和可修改的位数
	var getPhone = function(){
		var startp = $("#Sphone").text().trim();
		var endp = $("#Ephone").text().trim();
		
		function getV(avg0,avg1){
			for(var i=0;i<avg0.length;i++){
				if(avg0[i] !== avg1[i]){
					return i;
				}
			}
		}
		
		var numdiff = getV(startp,endp);
		
		return {
			"startp":startp,
			"endp":endp,
			"numdiff":numdiff
		}
	}
	
	function stringChangArr(str,diff){
		var tempArr = [];
		tempArr.push(str.substring(0,diff));
		tempArr.push(str.substring(diff));
		return tempArr;
	}
	
	function arrayChangStr(arr){
		var tempStr = "";
		$.each(arr,function(i){
			tempstr += arr[i];
		})
		return tempStr;
	}
	
	function compareInput(way,num1,num2){
		if(way == 0){
			if(num1<num2){
                setmark(num2-num1);
			   return true;
			}else{
			   return false;
			}
		}else{
			if(num1>num2){
				setmark(num1-num2);
			   return true;
			}else{
			   return false;
			}
		}
		
	}
	
	$("span").click(function(){
		var thisobj = $(this);
		setClick(thisobj);	
	});
    
    $("input").on("input propertychange",function(){
    	var txt = $(this).val();
    	console.log(txt);
    	var txtreg = /^[0-9]{1,13}$/;
    	if(!txtreg.test(txt)) {
			$(this).val("");
		}
    });
     
	
	$("input").blur(function(){
		var Tobj = getPhone();
		var thisobj = $(this);
		var temp = "";
		var dataT;
		var tempval,tempother;
		var parentSpan = $(this).parent();
		var inputli = parentSpan.find("input");
		
		if(parentSpan.attr("id") == "Sphone"){
			dataT = 0;
			tempval = stringChangArr(Tobj.startp,Tobj.numdiff);
			tempother = stringChangArr(Tobj.endp,Tobj.numdiff);
		}else{
			dataT = 1;
			tempval = stringChangArr(Tobj.endp,Tobj.numdiff);
			tempother = stringChangArr(Tobj.startp,Tobj.numdiff);
		}
		
		var Inputval = $(this).val().trim();
		console.log(Inputval.length);
		console.log($(this).attr("maxlength"));
		if(Inputval.length == $(this).attr("maxlength")){
			console.log("meimaobing");
			var bol = compareInput(dataT,Inputval,tempother[1]);
			console.log(bol);
			if(!bol){
				return ;
			}

		}else{
			return false;
		}
		
		for(var i=0; i<inputli.length; i++){
			temp += parentSpan.find("input").eq(i).val().trim();			
		}
	 
		parentSpan.contents().filter(function() {
    				return this.nodeType === 3;
					}).eq(0)[0].nodeValue = temp;
	
		parentSpan.removeClass("hideS").addClass("showS");
		parentSpan.find("input").removeClass("showI").addClass("hideI");
		
		parentSpan.find("input").eq(0).animate({
			left: '5px'
		})

	})
	
	$("input").keydown(function(event){	
		if(event.which == 13){
		   $(this).blur();	
		}
	});
	
	function setmark(num){
      $("mark.ztips").html("共"+(num+1)+"个号段");
	}
	
}
